const formRules = (rules, form, callback) => {
  for (let key in rules) {
    const value =
      typeof form[key] == "number" ? form[key].toString() : form[key];
    const rs = rules[key];
    for (let i = 0; i < rs.length; i++) {
      if (rs[i].required) {
        if (typeof value === "boolean" && value === false) {
          wx.showToast({
            title: rs[i].message,
            icon: "none",
          });
          callback(false);
          return;
        } else if (typeof value === "string" && value.trim() == "") {
          wx.showToast({
            title: rs[i].message,
            icon: "none",
          });
          callback(false);
          return;
        }
      } else if (rs[i].pattern && !rs[i].pattern.test(value)) {
        wx.showToast({
          title: rs[i].message,
          icon: "none",
        });
        callback(false);
        return;
      }
    }
  }
  callback(true);
};

// 获取输入值
const inputValue = (e) => {
  const param = e.currentTarget.dataset.name; //字段名
  const value = e.detail; //值
  const key = `formData.${param}`;
  const msg = `checkData.${param}`;
  this.setData({
    [key]: value,
    [msg]: "", //清空错误提示
  });
};

export { formRules, inputValue };
